﻿/*
 *  Copyright 2012 Cloudbase Solutions Srl
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the Microsoft Public License (Ms-PL).  
 */

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Management.Client;
using Cloudbase.Iis.HostsFileManager.Model;
using Cloudbase.Iis.HostsFileManager.Common;
using System.Collections;
using Microsoft.Web.Management.Server;
using System.Net;

namespace Cloudbase.Iis.HostsFileManager.Client
{
    internal class HostsFileModuleServiceProxy : ModuleServiceProxy, IHostsFileModuleService
    {
        public bool ContainsHost(string host)
        {
            return (bool)Invoke("ContainsHost", new object[] { host });
        }

        public void AddHost(string host, IPAddress ipAddress, string comment)
        {
            Invoke("AddHost", new object[] { host, ipAddress.ToString(), comment});
        }

        public void UpdateHost(string originalHost, string host, IPAddress ipAddress, string comment)
        {
            Invoke("UpdateHost", new object[] { originalHost, host, ipAddress.ToString(), comment });
        }

        public IList<HostFileEntry> GetAllEntries()
        {
            IList<HostFileEntry> l = new List<HostFileEntry>();

            foreach (var o in (IList)Invoke("GetAllEntries"))
            {
                PropertyBag pb = (PropertyBag)o;
                l.Add(new HostFileEntry()
                {
                    Host = (string)pb[0],
                    IPAddress = IPAddress.Parse((string)pb[1]),
                    Comment = (string)pb[2]
                });
            }

            return l;
        }

        public string GetHostsFilePath()
        {
            return (string)Invoke("GetHostsFilePath");
        }

        public void RemoveHost(string host)
        {
            Invoke("RemoveHost", new object[] { host });
        }

    }
}
